package drds.global_transaction.common.exception;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FrameworkException extends RuntimeException
{
	private static final long serialVersionUID = 5531074229174745826L;

	private final FrameworkErrorCode frameworkErrorCode;

	public FrameworkException()
	{
		this(FrameworkErrorCode.UnknownAppError);
	}

	public FrameworkException(FrameworkErrorCode err)
	{
		this(err.errMessage, err);
	}

	public FrameworkException(String msg)
	{
		this(msg, FrameworkErrorCode.UnknownAppError);
	}

	public FrameworkException(String msg, FrameworkErrorCode errCode)
	{
		this(null, msg, errCode);
	}

	public FrameworkException(Throwable cause, String msg, FrameworkErrorCode frameworkErrorCode)
	{
		super(msg, cause);
		this.frameworkErrorCode = frameworkErrorCode;
	}

	public FrameworkException(Throwable th)
	{
		this(th, th.getMessage());
	}

	public FrameworkException(Throwable th, String msg)
	{
		this(th, msg, FrameworkErrorCode.UnknownAppError);
	}

	public static FrameworkException nestedException(Throwable e)
	{
		return nestedException("", e);
	}

	public static FrameworkException nestedException(String msg, Throwable e)
	{
		log.error(msg, e.getMessage(), e);
		if (e instanceof FrameworkException)
		{
			return (FrameworkException) e;
		}

		return new FrameworkException(e, msg);
	}

	public FrameworkErrorCode getFrameworkErrorCode()
	{
		return frameworkErrorCode;
	}
}
